package com.gymmsystem.GYMMSystem.service.impl;

import com.gymmsystem.GYMMSystem.entity.Announcement;
import com.gymmsystem.GYMMSystem.mapper.AnnouncementMapper;
import com.gymmsystem.GYMMSystem.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AnnouncementServiceImpl implements AnnouncementService  {

    @Autowired
    private AnnouncementMapper announcementMapper;

    /** 
     * 获取所有已发布且已到定时发布时间的公告
     */
    @Override
    public List<Announcement> findAllAnnouncements() {
        return announcementMapper.findAllAnnouncements();
    }

    /**
     * 获取所有公告（排除已删除的）
     */
    @Override
    public List<Announcement> selectAllAnnouncements() {
        return announcementMapper.selectAllAnnouncements();
    }

    /**
     * 根据ID更新公告信息
     */
    @Override
    public boolean updateAnnouncementById(Announcement announcement) {
        if (announcement == null || announcement.getId() == null) {
            return false;
        }

        // 校验是否存在
        if (announcementMapper.selectById(announcement.getId()) == null) {
            return false;
        }

        // 自动更新时间
        announcement.setUpdatedAt(LocalDateTime.now());

        int result = announcementMapper.updateAnnouncementById(announcement);
        return result > 0;
    }

    /**
     * 根据ID逻辑删除公告
     */
    @Override
    public boolean deleteAnnouncementById(Long id) {
        int result = announcementMapper.deleteAnnouncementById(id);
        return result > 0;
    }

    /**
     * 根据ID获取公告详情
     */
    @Override
    public Announcement getAnnouncementById(Long id) {
        if (id == null) {
            return null;
        }
        return announcementMapper.selectById(id);
    }

    /**
     * 创建新公告
     */
    @Override
    public Long createAnnouncement(Announcement announcement) {
        if (announcement == null ||
                announcement.getTitle() == null || announcement.getTitle().trim().isEmpty() ||
                announcement.getContent() == null || announcement.getContent().trim().isEmpty()) {
            return null;
        }

        // 设置默认值
        if (announcement.getStatus() == null) {
            announcement.setStatus(0); // 草稿
        }
        if (announcement.getType() == null) {
            announcement.setType("运营通知");
        }
        LocalDateTime now = LocalDateTime.now();
        if (announcement.getCreatedAt() == null) {
            announcement.setCreatedAt(now);
        }
        if (announcement.getUpdatedAt() == null) {
            announcement.setUpdatedAt(now);
        }

        // 处理预约发布时间
        if (announcement.getScheduledPublishTime() != null) {
            // 确保时间格式正确，这里可以添加时间格式验证
            // LocalDateTime 会自动处理 ISO 格式字符串
        } else if (announcement.getStatus() == 1) {
            // 如果是立即发布，设置发布时间为当前时间
            announcement.setScheduledPublishTime(now);
        }

        int result = announcementMapper.insert(announcement);
        return result > 0 ? announcement.getId() : null;
    }

    /**
     * 发布公告
     */
    @Override
    public boolean publishAnnouncement(Long id) {
        if (id == null) {
            return false;
        }
        Announcement announcement = announcementMapper.selectById(id);
        announcement.setStatus(1); // 已发布状态
        announcement.setScheduledPublishTime(LocalDateTime.now()); // 立即发布

        int result = announcementMapper.updateAnnouncementById(announcement);
        return result > 0;
    }

    /**
     * 撤回公告
     */
    @Override
    public boolean withdrawAnnouncement(Long id) {
        if (id == null) {
            return false;
        }
        Announcement announcement = announcementMapper.selectById(id);
        announcement.setStatus(0); // 撤回为草稿状态

        int result = announcementMapper.updateAnnouncementById(announcement);
        return result > 0;
    }
}